﻿using System;
using System.Collections.Generic;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;

namespace DIaLOGIKa.b2xtranslator.ZipUtils
{
    public class DocxDataSource : IStaticDataSource
    {
        protected byte[] _bytes;
        public DocxDataSource()
        {
            _bytes = new byte[0];
        }

        public DocxDataSource(byte[] bytes)
        {
            _bytes = bytes;
        }

        public int Count { get { return _bytes.Length; } }

        public bool DiscardNullValue { get; set; }

        public void Append(byte[] bytes)
        {
            Array.ForEach(bytes, AddByte);
        }

        protected void AddByte(byte bt)
        {
            if (DiscardNullValue)
            {
                if (bt != 0)
                {
                    Add(bt);
                }
            }
            else
            {
                Add(bt);
            }
        }

        protected void Add(byte b)
        {
            Array.Resize(ref _bytes, _bytes.Length + 1);
            _bytes.SetValue(b, _bytes.Length - 1);
        }

        public System.IO.Stream GetSource()
        {
            return new MemoryStream(_bytes);
        }
    }
}
